#include "BooleanOperationWidget.h"

#include "Core/MainWindow.h"
#include <QMessageBox>
#include "FeatureShape/HistoryManager.h"
#include "FeatureShape/FeatureShape.h"


BooleanOperationWidget::BooleanOperationWidget(MainWindow* inMainWindow, ShapeOperation* inOperation, QWidget* parent)
	: QRSWidgetBase(inMainWindow, inOperation, parent)
	, ui(new Ui::BooleanOperationWidgetClass())
{
	ui->setupUi(this);

	QPixmap tempErrorImg = QPixmap(".//Resource//error.png");
	ui->targetImg->setPixmap(tempErrorImg);
	ui->targetImg->setMaximumSize(QSize(20, 20));
	ui->targetImg->setScaledContents(true);
	ui->toolImg->setPixmap(tempErrorImg);
	ui->toolImg->setMaximumSize(QSize(20, 20));
	ui->toolImg->setScaledContents(true);

	ui->booleanSelect->addItem("和运算(union)");
	ui->booleanSelect->addItem("差运算(subtraction)");
	ui->booleanSelect->addItem("交运算(intersection)");

	ui->targetSelSubWidget->setStyleSheet("background-color:rgb(255,178,102)");

	ui->confirmButton->setEnabled(false);
	ui->modifyButton->setEnabled(false);

	connect(m_mainWindow, &MainWindow::OnSelectedActor, this, &BooleanOperationWidget::OnSelectedActor);

	connect(ui->cancelButton, &QPushButton::clicked, this, &BooleanOperationWidget::close);
	connect(ui->confirmButton, &QPushButton::clicked, this, &BooleanOperationWidget::OnConfirm);
	connect(ui->modifyButton, &QPushButton::clicked, this, &BooleanOperationWidget::OnModify);
	connect(ui->targetSelect, &QPushButton::clicked, this, &BooleanOperationWidget::OnCancelSelTarget);
	connect(ui->toolSelect, &QPushButton::clicked, this, &BooleanOperationWidget::OnCancelSelTool);

	connect(ui->booleanSelect, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &BooleanOperationWidget::OnBoolOPChange);

	m_mainWindow->SetMaxSelectedActors(2);
}

BooleanOperationWidget::~BooleanOperationWidget()
{
	delete ui;
}

void BooleanOperationWidget::OnSelectedActor(Handle(QIVtkSelectionPipeline) actor)
{
	if (m_selIndex == 1)
	{
		m_selectedActorTarget = actor;
		m_selIndex++;
		// 改变背景颜色
		ui->targetSelSubWidget->setStyleSheet("background-color:rgb(255,255,255)");
		ui->toolSelSubWidget->setStyleSheet("background-color:rgb(255,178,102)");

		if (!m_selectedActorTool.IsNull())
		{
			ui->modifyButton->setEnabled(true);
		}

		QPixmap tempOKImg = QPixmap(".//Resource//right.png");
		ui->targetImg->setPixmap(tempOKImg);
	}
	else
	{
		m_selectedActorTool = actor;
		ui->modifyButton->setEnabled(true);

		QPixmap tempOKImg = QPixmap(".//Resource//right.png");
		ui->toolImg->setPixmap(tempOKImg);
	}
}

void BooleanOperationWidget::closeEvent(QCloseEvent* event)
{
	// 清除已选择的Actor
	m_mainWindow->ClearSelectedActors();

	// 没有点确定，删除创建的Actor
	if (!bIsConfirmed)
	{
		if (!m_selectedActorTarget.IsNull())
		{
			m_selectedActorTarget->SetPipelineVisibility(true);
		}
		if (!m_selectedActorTool.IsNull())
		{
			m_selectedActorTool->SetPipelineVisibility(true);
		}
		if (!m_resultActor.IsNull())
		{
			m_mainWindow->RemoveActorFromRenderer(m_resultActor);
			m_mainWindow->FlushRender();
		}
	}

	m_mainWindow->SetSubWidgetSelected(false);
	m_mainWindow->SubWidgetClose(bIsConfirmed);
	m_mainWindow->ResetMaxSelectedActors();
	disconnect(m_mainWindow, &MainWindow::OnSelectedActor, this, &BooleanOperationWidget::OnSelectedActor);
}

void BooleanOperationWidget::OnConfirm()
{
	// 获得下标index
	int targetShapeIndex = GetIndexFromActor(m_mainWindow->GetCachedActors(), m_selectedActorTarget);
	int toolShapeIndex = GetIndexFromActor(m_mainWindow->GetCachedActors(), m_selectedActorTool);

	// 创建的Actor有效
	if (!m_resultActor.IsNull())
	{
		m_mainWindow->OnActorConfirmCreate(m_resultActor);
		m_resultActor->m_featureShape.reset(new FeatureShapeMix(m_resultActor->OccShape()));
	}

	// 确定创建->删除源Actor
	m_mainWindow->RemoveActorFromRenderer(m_selectedActorTarget);
	m_mainWindow->RemoveActorFromRenderer(m_selectedActorTool);

	bIsConfirmed = true;

	BooleanOperation* op = new BooleanOperation("布尔运算", targetShapeIndex, toolShapeIndex, m_op);
	m_mainWindow->GetHistoryManager()->applyOperation(op);

	close();
}

void BooleanOperationWidget::OnModify()
{
	if (bIsModify)
	{
		return;
	}
	
	TopoDS_Shape resultShape = MainWindow::BooleanOperation(m_selectedActorTarget->OccShape(), m_selectedActorTool->OccShape(), m_op);
	if (resultShape.IsNull())
	{
		QMessageBox::warning(this, "警告", "布尔运算所得结果为空!!!");
		return;
	}

	bIsModify = true;
	ui->confirmButton->setEnabled(true);

	m_resultActor = m_mainWindow->createBasicUnitActor(resultShape);
	// 暂时隐藏运算的Actor
	m_selectedActorTarget->SetPipelineVisibility(false);
	m_selectedActorTool->SetPipelineVisibility(false);

	ui->booleanSelect->setEnabled(false);

	m_mainWindow->FlushRender();
}

void BooleanOperationWidget::OnCancelSelTarget()
{
	// 已经修改不能取消选中
	if (!m_selectedActorTarget.IsNull() && !bIsModify)
	{
		m_mainWindow->RemoveSelectedActor(m_selectedActorTarget);
		m_selectedActorTarget = nullptr;
		if (m_selIndex == 2)
		{
			m_selIndex--;
			ui->targetSelSubWidget->setStyleSheet("background-color:rgb(255,178,102)");
			ui->toolSelSubWidget->setStyleSheet("background-color:rgb(255,255,255)");
		}
		ui->modifyButton->setEnabled(false);

		QPixmap tempErrorImg = QPixmap(".//Resource//error.png");
		ui->targetImg->setPixmap(tempErrorImg);
	}
}

void BooleanOperationWidget::OnCancelSelTool()
{
	// 已经修改不能取消选中
	if (!m_selectedActorTool.IsNull() && !bIsModify)
	{
		m_mainWindow->RemoveSelectedActor(m_selectedActorTool);
		ui->modifyButton->setEnabled(false);

		QPixmap tempErrorImg = QPixmap(".//Resource//error.png");
		ui->toolImg->setPixmap(tempErrorImg);
	}
}

void BooleanOperationWidget::OnBoolOPChange(int index)
{
	switch (index)
	{
	case 0:
		m_op = BooleanOP::Union;
		break;
	case 1:
		m_op = BooleanOP::Subtract;
		break;
	case 2:
		m_op = BooleanOP::Intersect;
		break;
	default:
		break;
	}
}
